package sg.edu.nus.iss.cats.controller;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;



public class LoginServlet extends HttpServlet {

    public void doGet(HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException {
            
     
       HttpSession userSession = request.getSession(false);
       if (userSession == null || userSession.getAttribute("USERSESSION") == null){       
            //Display login form
            HTMLUtil.displayPageHeader(request,response);
            HTMLUtil.displayLoginForm(request,response);
            HTMLUtil.displayPageFooter(request,response);
            return;
       
       }else {
       
            doPost(request,response);
       }
       
            
    }
  
  
    public void doPost(HttpServletRequest request,HttpServletResponse response)
            throws ServletException, IOException {
    
        //Get userId and password from the login form
        String userId = request.getParameter("username");
        String password = request.getParameter("password");

        // Check if userId or password is null or blank, then prompt to enter userId
        System.out.println("UserId:" + userId + "password: " + password);
        if (userId == null || password == null || userId.equals("") || password.equals("")){ 
                System.out.println("UserId or password are null"); 
                PrintWriter out = response.getWriter();  
                HTMLUtil.displayException(request,response,"Please verify the userId and password........");
                HTMLUtil.displayLoginForm(request,response);
                out.close();
                return;
        }else{
                         
              //Validate the password and create a userSession with key userid
               UserSession userSession = null;
               
               try {
               
                  UserSessionManager sessionMgr = new UserSessionManager();
                  userSession= sessionMgr.validate(userId, password);
                  
               }catch(Exception e){
                    PrintWriter out = response.getWriter();        
                    HTMLUtil.displayException(request,response,"Login failed. Please verify userId and password........");
                    HTMLUtil.displayLoginForm(request,response);
                    out.close();
                    return;              
               }

                 // If user is valid,create a session for the user.
               HttpSession session = request.getSession(true);
               session.setAttribute("USERSESSION",userSession);

             
              //By default show the user's information              
              RequestDispatcher rd = getServletContext().getRequestDispatcher(  
                                    "/showUser/user/" + userSession.getUser().getUserId());
                                    
              // Check if user has admin role then forward to modifyRoles page             
              if (userSession.isAdmin()){                
                rd = getServletContext().getRequestDispatcher( 
                                        "/admin/view");
              }

              if (rd != null){
                  rd.forward(request,response);
              }
        
        }
        
    }
    
        

}